/* Copyright (c) 2004-2022 peigen.info. All rights reserved. */

package info.peigen.hotpot.common.core.result;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import info.peigen.hotpot.common.core.enums.Messageable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <b>(ResultStatus)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2021/4/9
 */
@Getter
@NoArgsConstructor
@AllArgsConstructor
public enum ResultStatus implements Messageable {

    /** 初始化 */
    initialization("initialization", "初始化"),

    /** 成功 */
    success("success", "成功"),

    /** 失败 */
    failure("failure", "失败"),

    /** 处理中 */
    processing("processing", "处理中"),

    /** 不晓得啷格回事 */
    unknown("unknown", "不晓得啷格回事"),

    ;

    /** 枚举值 */
    private String code;

    /** 枚举描述 */
    private String message;


    /**
     * 通过枚举<code>code</code>获得枚举
     *
     * @param code 枚举值
     *
     * @return ResultStatus
     */
    public static ResultStatus getByCode(String code) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equals(code, innerEnum.getCode()))
                .findFirst().orElse(unknown);
    }

    /**
     * 通过枚举<code>name</code>获得枚举
     *
     * @param name 枚举名
     *
     * @return ResultStatusResultStatus
     */
    public static ResultStatus getByName(String name) {
        return Stream.of(values())
                .filter(innerEnum -> StrUtil.equalsIgnoreCase(name, innerEnum.name()))
                .findFirst().orElse(unknown);
    }

    /**
     * 获取全部枚举
     *
     * @return List<ResultStatus>
     */
    public static List<ResultStatus> getAllEnum() {
        return Lists.newArrayList(values());
    }

    /**
     * 获取全部枚举值
     *
     * @return List<String>
     */
    public static List<String> getAllEnumCode() {
        return getAllEnum().stream().map(ResultStatus::code).collect(Collectors.toList());
    }

    /**
     * 通过code获取msg
     *
     * @param code 枚举值
     *
     * @return String
     */
    public static String getMsgByCode(String code) {

        return Optional.ofNullable(code).map(
                str ->
                        Optional.ofNullable(getByCode(str))
                                .map(ResultStatus::message).orElse(unknown.message())
        ).orElse(null);
    }

    /**
     * 获取枚举code
     *
     * @param innerEnum 枚举
     *
     * @return code
     */
    public static String getCode(ResultStatus innerEnum) {
        return Optional.ofNullable(innerEnum).map(ResultStatus::code).orElse(unknown.code());
    }

    /**
     * map
     *
     * @return Map
     */
    public static Map<String, String> maps() {

        return Stream.of(values()).collect(Collectors.toMap(ResultStatus::code, ResultStatus::message));
    }

    /**
     * @return Returns the code.
     */
    @Override
    public String code() {
        return code;
    }

    /**
     * @return Returns the message.
     */
    @Override
    public String message() {
        return message;
    }

}